#include "DSearchResultItem.h"

#include "DUtils.h"

DSearchResultItem::DSearchResultItem(const DUIHomeworkItem& item,
                                     QWidget* parent)
    : QFrame(parent)
{
    QVBoxLayout* layoutMain = new QVBoxLayout();
    QLabel* labName = new QLabel(QString::fromStdString(
        item.student.strNumber + " |" + item.student.strName));
    labName->setStyleSheet("QLabel{color:blue;font-weight: bold;}");
    layoutMain->addWidget(labName);

    const int nYes =
        std::count_if(item.vecSubmitAll.begin(), item.vecSubmitAll.end(),
                      [](const DUISubmit& i) { return i.bSubmit; });

    const int nNo =
        std::count_if(item.vecSubmitAll.begin(), item.vecSubmitAll.end(),
                      [](const DUISubmit& i) { return !i.bSubmit; });

    QString strSubmit;
    if (item.bYes)
    {
        strSubmit += QString("已交:%1/%2 ").arg(nYes).arg(nYes + nNo);
    }

    if (item.bNo)
    {
        strSubmit += QString("未交:%1/%2 ").arg(nNo).arg(nYes + nNo);
    }

    QHBoxLayout* layoutSubmit = new QHBoxLayout();
    QLabel* lablSubmit = new QLabel(strSubmit);
    layoutSubmit->addWidget(lablSubmit);
    lablSubmit->setStyleSheet("QLabel{color:red;font-weight: bold;}");
    QPushButton* btnDetail = new QPushButton(">>");
    btnDetail->setStyleSheet("QPushButton{color:blue;font-weight: bold;}");
    connect(btnDetail, &QPushButton::clicked,
            [item, this] { emit signalDetail(item); });

    btnDetail->setFixedSize(40, 20);
    layoutSubmit->addWidget(btnDetail);

    layoutMain->addLayout(layoutSubmit);

    setLayout(layoutMain);
    setObjectName("SearchResultItem");

    DUtils::setStyleSheet(this, ":/HomeworkItem.qss");
}
